
$(function() {
	$("input[id^='code_s']:visible").autocomplete(Test.mold_code);
	
	$("input[id^='customer_s']:visible").autocomplete(Test.customer);

	$("input[id^='engineer_s']:visible").autocomplete(Test.emp);
	
	_ck_default("input[id^='engineer_s']:visible", "input[id^='engineer_t']:visible");

	Tip.emp('业务员');
	
	$("select[id^='emp_t_s']:visible").change(function() {
		var label = $("label[id^='label_emp_t']:visible");
		var a = $(this).val();
		if (a == 'emp') {
			label.html("业务员：");
		} else if (a == 'oper') {
			label.html("录入员：");
		}
	});
	
	Tip.supplier();
});

var Mold = {
	dialog_add_: function (url, title, xy) {
		var result = Common.result();
		$.getJSON('mold.php?do=Mold.audit&item=76', function(json) {
			if (json.status == 1) {
				$('#dialog_add').dialog('option', 'buttons', {			
					"关闭": function() { 
						$(this).dialog("close");
					},
					
					"添加": function() {
						if (!confirm("确认是否提交？")) {
							return false;
						}
			
						var form = $("#form_add")[0];
			
						var body = { };
						body.customer = form.customer.value;
						body.date = form.date.value;
						
						body.code = form.code.value;
						body.category = form.category.value;
						
						body.emp = form.emp.value;
						body.content = form.content.value;
						
						$(this).dialog("close");
			
						var url = 'mold.php?do=Mold.addPost';
			
						$.post(url, body, function(data) {
							$(result).html(data);
						});
					}
				});
			
				dialog_add(url, title, xy);
			} else {
				_error();
			}
		});
	},

	dialog_import: function (url, title, xy) {
		var result = Common.result();
		$.getJSON('mold.php?do=Mold.audit&item=76', function(json) {
			if (json.status == 1) {
				$('#dialog_add').dialog('option', 'buttons', {
					"关闭": function() {
						$(this).dialog("close");
					},

					"导入": function() {
						if (!confirm("确认是否导入？")) {
							return false;
						}
						
						var form = $("#form_add")[0];

						var body = { };
						body.data = form.data.value;
						
						var type = form.type.value;
						
						if (type != null && type == '2') {
							var s = "mold.php?do=Mold.ajax_upload";
							$('#userfile').uploadifySettings('script', s);
							$('#userfile').uploadifyUpload();
							return false;
						} 

						$(this).dialog("close");

						$.post('mold.php?do=Mold.importPost', body, function(data) {
							$(result).html(data);
						});
					}
				});

				dialog_add(url, title, xy);
			} else {
				_error();
			}
		});
	},

	popup: function (e, p2, id, status) {
		$.cookie('id_', id);

		p2 = query(p2);
		$.cookie('p2', p2);

		var c1 = 'mold.php?do=Mold.edit&id=' + id + p2;
		var c2 = 'mold.php?do=Mold.view&id=' + id + p2;
		
		var c3 = 'mold.php?do=Mold.outer&id=' + id;

		var a1 = ['编辑信息', 'url', 'Mold.dialog_edit_', '650x400'];
		var a2 = ['查看信息', 'url', 'Common.dialog_view_', '650x400'];

		var a3 = ['开始制造', 'url', 'Mold.dialog_outer', '650x400'];

		if (status < 9) {
			var s = c1 + ", " + c2;

			return this.popup_(e, [a1, a2], s);
		}

		if (status == 9) {
			var s = c2;

			return this.popup_(e, [a2], s);
		}

		var s = c2;

		return this.popup_(e, [a2], s);
	},

	popup_: function (e, a, s) {
		var id = $.cookie('id_');
		
		var c3 = 'mold.php?do=Mold.copy&id=' + id;
		var a3 = ['复制信息', 'url', 'Mold.dialog_copy_', '650x400'];
		
		var ss = 'mold.php?do=Mold.ss&id=' + id;
		var bb = ['修改状态', 'url', 'Mold.dialog_ss', '400x200'];
		
		s += ", " + c3 + ", " + ss;
		
		a.push(a3);
		a.push(bb);
			
		return popup(e, a, s);
	},

	/**
	 * 修改状态
	 */
	dialog_ss: function (url, title, xy) {
		var result = Common.result();
		$.getJSON('mold.php?do=Mold.audit&item=25', function(json) {
			if (json.status == 1) {
				$('#dialog_edit').dialog('option', 'buttons', {
					"关闭": function() {
						$(this).dialog("close");
					},

					"修改": function() {
						if (!confirm("确认是否提交？")) {
							return false;
						}

						var form = $("#form_edit")[0];

						var body = {};
						body.id = form.id.value;
						body.field = form.field.value;
						body.data = form.data.value;
						body.status = form.status.value;

						$(this).dialog("close");

						var url = 'mold.php?do=Mold.ssPost';
						var p2 = $.cookie('p2');
						if (p2 != null) {
							url += p2;
						}

						$.post(url, body, function(data) {
							$(result).html(data);
						});
					}
				});

				dialog_edit(url, title, xy);
			} else {
				_error();
			}
		});
	},

	dialog_edit_: function (url, title, xy) {
		var result = Common.result();
		$.getJSON('mold.php?do=Mold.ajax_emp', function(json) {
			if (json.status == 1) {
				$('#dialog_edit').dialog('option', 'buttons', {
					"关闭": function() { 
						$(this).dialog("close");
					},
					
					"修改": function() {
						if (!confirm("确认是否提交？")) {
							return false;
						}

						var form = $("#form_edit")[0];

						var body = {};
						body.id = form.id.value;
						
						body.customer = form.customer.value;
						body.code = form.code.value;
						body.category = form.category.value;
						body.date = form.date.value;
						
						body.supplier = form.supplier.value;
						body.supplier_x = Supplier.supplier_x(form);
						
						body.sm = form.sm.value;
						body.content = form.content.value;
												
						body.engineer = form.engineer.value;
						body.emp = form.emp.value;
						
						body.status = form.status.value;

						$(this).dialog("close");
						
						var url = 'mold.php?do=Mold.editPost';
						var p2 = $.cookie('p2');
						if (p2 != null) {
							url += p2;
						}

						$.post(url, body, function(data) {
							$(result).html(data);
						});
					}
				});

				dialog_edit(url, title, xy);
			} else {
				_error();
			}
		});
	},
	
	dialog_copy_: function (url, title, xy) {
		var result = Common.result();
		$.getJSON('mold.php?do=Mold.audit&item=76', function(json) {
			if (json.status == 1) {
				$('#dialog_edit').dialog('option', 'buttons', {
					"关闭": function() { 
						$(this).dialog("close");
					},
					
					"复制": function() {
						if (!confirm("确认是否提交？")) {
							return false;
						}

						var form = $("#form_edit")[0];

						var body = {};
						
						body.customer = form.customer.value;
						body.date = form.date.value;
						
						body.code = form.code.value;
						body.category = form.category.value;
						
						body.emp = form.emp.value;
						body.content = form.content.value;

						$(this).dialog("close");
						
						var url = 'mold.php?do=Mold.addPost';
						var p2 = $.cookie('p2');
						if (p2 != null) {
							url += p2;
						}

						$.post(url, body, function(data) {
							$(result).html(data);
						});
					}
				});

				dialog_edit(url, title, xy);
			} else {
				_error();
			}
		});
	},

	dialog_outer: function (url, title, xy) {
		var result = Common.result();
		$.getJSON('mold.php?do=Mold.audit&item=72', function(json) {
			if (json.status == 1) {
				$('#dialog_add').dialog('option', 'buttons', {
					"关闭": function() { 
						$(this).dialog("close");
					},
					
					"开始制造": function() {
						if (!confirm("确认是否提交？")) {
							return false;
						}

						var form = $("#form_add")[0];

						var body = {};
						body.id = form.id.value;
						body.engineer = form.engineer.value;
						body.supplier = form.supplier.value;
						body.supplier_x = Supplier.supplier_x(form);
						
						body.amount = form.amount.value;
						body.currency = form.currency.value;
						body.rate = form.rate.value;
						body.rmb = form.rmb.value;
						
						body.content = form.content.value;

						$(this).dialog("close");
						
						var url = 'mold.php?do=Mold.outerPost';

						$.post(url, body, function(data) {
							$(result).html(data);
						});
					}
				});

				dialog_add(url, title, xy);
			} else {
				_error();
			}
		});
	},
	
	dialog_add_plan: function (url, title, xy) {
		var result = Common.result();
		$('#dialog_add').dialog('option', 'buttons', {
			"关闭": function() { 
				$(this).dialog("close");
			},
			
			"添加": function() {
				if (!confirm("确认是否提交？")) {
					return false;
				}

				var form = $("#form_add")[0];

				var body = {};
				body.id = form.id.value;
				body.piece = form.piece.value;
				body.technics = form.technics.value;
				body.emp = form.emp.value;
				body.content = form.content.value;

				body.t1 = form.t1.value;
				body.t2 = form.t2.value;

				body.hour = form.hour.value;
				body.minute = form.minute.value;

				$(this).dialog("close");
				
				var url = 'mold.php?do=Plan.addPost';

				$.post(url, body, function(data) {
					alert("添加成功！");
				});
			}
		});

		dialog_add(url, title, xy);
	},

	list_end: function (url, view) {
		$.getJSON('mold.php?do=Mold.ajax_engineer', function(json) {
			if (json.status == 1) {
				if (confirm("是否要完成生产？")) {
					list(url, view);
				}
			} else {
				_error();
			}
		});	
	},

	list_test: function (url, view) {
		$.getJSON('mold.php?do=Mold.ajax_engineer', function(json) {
			if (json.status == 1) {
				if (confirm("是否要开始试模？")) {
					list(url, view);
				}
			} else {
				_error();
			}
		});	
	},

	list_test_end: function (url, view) {
		$.getJSON('mold.php?do=Mold.ajax_engineer', function(json) {
			if (json.status == 1) {
				if (confirm("是否要完成试模？")) {
					list(url, view);
				}
			} else {
				_error();
			}
		});	
	},

	dialog_info: function (url, title, xy) {
		$('#dialog_add').dialog('option', 'buttons', {
			"关闭": function() { 
				$(this).dialog("close");
			},
			
			"添加": function() {
				if (!confirm("确认是否提交？")) {
					return false;
				}

				var form = $("#form_add")[0];

				var body = { };
				body.id = form.id.value;
				body.customer = form.customer.value;
				body.title = form.title.value;
				body.content = form.content.value;

				var url = 'mold.php?do=Info.addPost';

				$.post(url, body, function(data) {
					var a = $.evalJSON(data);

					var s = "mold.php?do=Info.ajax_upload&id=" + a.id + "&name=" + $("#filename").val();
					$('#userfile').uploadifySettings('script', s);

					if ($("#_upload").val() == "1") {
						$("#dialog_add").dialog("close");
					} else {
						$('#userfile').uploadifyUpload();
					}
				});
			}
		});

		dialog_add(url, title, xy);
	},
	
	popup_all: function (e, p2) {
		p2 = query(p2);
		$.cookie('p2', p2);

		var c1 = 'mold.php?do=Mold.delete' + p2;	
		var c9 = 'mold.php?do=Mold.xls' + p2;	
			
		var a1 = ['删除信息', 'url', 'Common.list_delete'];				
		var a9 = ['导出Excel', 'url', 'jswin', '1024x768'];

		var s = c1 + ", " + c9;

		return popup(e, [a1, a9], s);
	}	
};
